<?php
/**
 * @page filter_module Filter Module
 * 
 * Filter module can create links as per each field's option. In the image below, we have a Laptop 
 * category with a dropdown field "Operating system", and filter is shown on the right side along
 * with various other filters. This module can improve you site's usability to a great extent.
 * 
 * @image html filter_module.jpg
 * 
 */
class FilterModule extends GigaModule
{
	var $name = 'Filtering Options';
	
	var $pageProperties = array(
		'before' => '',
		'after' => ''
	);
	
	var $configuration = array(
	);

	function __construct()
	{
		$this->configuration = array(
		'before_text' => array(
			'type' => 'text',
			'label' => "Before Text (optional)",
			'tooltip' => __('Text to appear on top of module, before the range.')
		), 
		'category' => array(
			'type' => 'select', 
			'empty' => __('-Select-'),
			'label' => 'Select Category', 
			'options' => array(),
			'triggers' => array('onChange' => array('update' => 'filter_field', 'from_model' => 'FilterOption')),
			'validate'=>VALID_NOT_EMPTY,
		),
		'filter_field' => array(
			'type' => 'select',
			'empty' => __('-Select-'),
			'label' => "Filter Field",
			'validate'=>VALID_NOT_EMPTY,
			'tooltip' => 'Available fields which can be used by this module'
		),
		'after_text' => array(
			'type' => 'text',
			'label' => "After Text (optional)",
			'tooltip' => 'Text to appear at bottom of module, after the range'
		), 
	);
		$categories = cri('ListCategory')->getOptions();
		$this->configuration['category']['options'] = $categories;
	}
	
	function render($params)
	{
		extract($params);
		$appHtml = ClassRegistry::getObject('view')->loaded['appHtml'];
		if($before_text)
		{
			$before_text = $before_text.'<br/><br/>';
		}
		if($after_text)
		{
			$after_text = '<br/>'.$after_text;
		}
		$output = $before_text.'<ul class="styled_list">';
		
		$listing = cri('Listing');
		$listing->setCategory($category);
		$options = $listing->getAttributes()->getPropertyValue($filter_field, 'options');

		foreach($options as $key => $option)
		{
			$link = $option;
			$href = '/listings/index/advancedSearch/'.$category.'/search_'.$filter_field.':'.$key;
			$output .= "<li>".$appHtml->link($link, $href) ." </li>";
		}
		
		$output .= "</ul>".$after_text;

		return $output;
	}
}
?>
